var t = getApp(), e = t.requirejs("core"), a = t.requirejs("foxui"),util = t.requirejs("util"), j = (t.requirejs("jquery"), t.requirejs("wxParse/wxParse"));


Page({
  data: {
    datas: {},
    article_content: '',
    showModal: false, // 显示modal弹窗
    sex: 1,// 1男 2女
    birth: '',
    realname: '',
    idcard: '',
    mobile: '',
    realname: '',
    a_id: '3',//活动id
  },

  onLoad: function (options) {
    
    this.setData({
      a_id: options.id ? options.id : 3,
    })
    this.getDetail(options.id ? options.id : 3);
  },

  // 导航
  goToAddress: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success (res) {
        wx.openLocation({
          latitude: Number(that.data.datas.lat),//要去地点的纬度
          longitude: Number(that.data.datas.lng),///要去地点的经度-地址
          scale: 18,
          name: that.data.datas.site,
          address: that.data.datas.address,
        })
      }
     })
  },

  getDetail: function (id) {
    var t = this;
    e.post("activity/get_detail", { id: id }, function (res) {
      if(res.res){
        t.setData({
          datas: res.res,
        })
        j.wxParse("wxParseData", "html", res.res.article_content, t, "0");
      }
      
    });
  },


  handleOpen(e) {
    // if(e.currentTarget.dataset.canbm == 0){
    //   this.showToast('报名已结束');
    //   return;
    // }
    this.setData({
      showModal: true,
    })
  },

  // 重置
  formReset: function (e) {
    this.setData({
      showModal: false,
    })
  },

  // 性别选择
  radioChange(e) {
    let value = e.detail.value;
    this.setData({
      sex: value
    })
  },

  // 出生日期选择
  bindDateChange: function (e) {
    this.setData({
      birth: e.detail.value
    })
  },

  showToast(text, icon = 'none') {
    wx.showToast({
      title: text,
      icon: icon,
      duration: 2000,
    });
  },


  // 报名
  handleConfirm(a) {
    var that = this;
    if (!a.detail.value.realname) {
      this.showToast('请填写姓名');
      return;
    }
    if (!a.detail.value.mobile) {
      this.showToast('请填写联系方式');
      return;
    }
    if (!e.checkPhone(a.detail.value.mobile)) {
      this.showToast('请填写正确的联系方式');
      return;
    }
    if (!a.detail.value.birth) {
      this.showToast('请选择出生日期');
      return;
    }
    if (!a.detail.value.idcard) {
      this.showToast('请填写身份证号码');
      return;
    }
    if (!e.checkIDCard(a.detail.value.idcard)) {
      this.showToast('请填写正确的身份证号码');
      return;
    }

    let parms = {
      openid: t.getCache("userinfo_openid"),
      realname: a.detail.value.realname,
      sex: this.data.sex,
      birth: a.detail.value.birth,
      idcard: a.detail.value.idcard,
      mobile: a.detail.value.mobile,
      a_id: this.data.a_id,
    }
    e.post("activity/sign_up", parms, function (res) {
      if (res.err == 0) {
        that.showToast('报名成功', 'success');
        setTimeout(() => {
          that.setData({
            showModal: false
          })
          wx.navigateTo({
            url: "/signUp/detail/index?id=" + res.id
        });
        }, 2000)

      } else {
        that.showToast(res.msg);
      }

    });
  }

})